package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FragmentTest extends Activity {
	public final static String TAG = "FragmentCycle";

	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "Activity- onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragmenttest);
	}

	protected void onStart() {
		Log.d(TAG, "Activity- onStart()");
		super.onStart();
	}

	protected void onResume() {
		Log.d(TAG, "Activity- onResume()");
		super.onResume();
	}

	protected void onPause() {
		Log.d(TAG, "Activity- onPause()");
		super.onPause();
	}

	protected void onStop() {
		Log.d(TAG, "Activity- onStop()");
		super.onStop();
	}

	protected void onDestroy() {
		Log.d(TAG, "Activity- onDestroy()");
		super.onDestroy();
	}

	public static class CounterFragment extends Fragment {
		TextView txtCounter;

		public void onAttach(Activity activity) {
			Log.d(TAG, "     Fragment- onAttach()");
			super.onAttach(activity);
		}

		public void onCreate(Bundle savedInstanceState) {
			Log.d(TAG, "     Fragment- onCreate()");
			super.onCreate(savedInstanceState);
		}

		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Log.d(TAG, "     Fragment- onCreateView()");
			View root = inflater.inflate(R.layout.counterfragment, container,
					false);
			Button btnIncrease = (Button) root.findViewById(R.id.btnincrease);
			txtCounter = (TextView) root.findViewById(R.id.txtcounter);
			
			if (savedInstanceState != null) {
				txtCounter.setText(Integer.toString(savedInstanceState
						.getInt("counter")));
			}
			
			Bundle bundle =getArguments();
			if(bundle !=null){
				txtCounter.setText(Integer.toString(bundle.getInt("start")));
			}

			btnIncrease.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					int cnt = Integer.parseInt(txtCounter.getText().toString());
					txtCounter.setText(Integer.toString(cnt + 1));
				}
			});
			return root;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			int a = Integer.parseInt(txtCounter.getText().toString());
			outState.putInt("counter", a);

		}

		public void onActivityCreated(Bundle savedInstanceState) {
			Log.d(TAG, "     Fragment- onActivityCreated()");
			super.onActivityCreated(savedInstanceState);
		}

		public void onStart() {
			Log.d(TAG, "     Fragment- onStart()");
			super.onStart();
		}

		public void onResume() {
			Log.d(TAG, "     Fragment- onResume()");
			super.onResume();
		}

		public void onPause() {
			Log.d(TAG, "     Fragment- onPause()");
			super.onPause();
		}

		public void onStop() {
			Log.d(TAG, "     Fragment- onStop()");
			super.onStop();
		}

		public void onDestroyView() {
			Log.d(TAG, "     Fragment- onDestroyView()");
			super.onDestroyView();
		}

		public void onDetach() {
			Log.d(TAG, "     Fragment- onDetach()");
			super.onDetach();
		}

	}
}
